/*
 * $Id: SignUpVWCommandValidator.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.web.support.validator;

import com.sci.logisticsmap.support.validator.BaseValidator;
import com.sci.logisticsmap.web.support.WebConstants;
import com.sci.logisticsmap.web.model.SignUpVWCommand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

import java.util.regex.Pattern;

/**
 * The class {@code ProfileVWCommandValidator} represents the validator
 * of {@link SignUpVWCommand} command.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
public class SignUpVWCommandValidator extends BaseValidator<SignUpVWCommand> {
    /**
     * The email pattern.
     */
    private final Pattern emailPattern;

    /**
     * The default constructor.
     * Initializes the validator.
     */
    public SignUpVWCommandValidator() {
         emailPattern = Pattern.compile(WebConstants.EMAIL_REGEXP, Pattern.CASE_INSENSITIVE);
    }

    /*
     * (non-Javadoc)
     *
     * @see BaseValidator#supports(Class)
     */
	@Override
    public boolean supports(Class<?> clazz) {
        return SignUpVWCommand.class.isAssignableFrom(clazz);
    }

    /*
     * (non-Javadoc)
     *
     * @see BaseValidator#validateInternal(Object, org.springframework.validation.Errors)
     */
	@Override
	protected void validateInternal(SignUpVWCommand command, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "field.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "field.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "field.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "field.required");

        if (StringUtils.isNotBlank(command.getUsername()) && !emailPattern.matcher(command.getUsername()).find()) {
            errors.rejectValue("username", "field.invalid.format");
        }

        if (StringUtils.isNotBlank(command.getPassword())) {
            if (command.getPassword().trim().length() < WebConstants.PASSWORD_MINIMUM_LENGTH) {
                errors.rejectValue("password", "field.min.length", new Object[] {Integer.valueOf(WebConstants.PASSWORD_MINIMUM_LENGTH)}, null);
            }
        }
    }
}
